import { 
  dygangUrls,
  contextMenusId
} from '../utils/status.js';
import request from '../utils/fetch.js';
console.log(request)
request.post('http://10.26.2.248:3001/api/login', {
  username: 'admin',
  password: 'admin123'
})
  .then(res => {
    console.log(res)
  })
  .catch(err => {
    console.log(err)
  })
// fetch('http://10.26.2.248:3001/api/login', {
//   method: 'post',
//   headers: {
//     'Content-Type': 'application/json'
//   },
//   body: JSON.stringify({
//     username: 'admin',
//     password: 'admin123'
//   })
// })
//   .then(res => res.json())
//   .then(data => {
//     console.log(data)
//   })
//   .catch(err => {
//     console.log(err)
//   })
// chrome.webRequest.onBeforeRequest.addListener((details) => {
//   console.log(details)
// }, {
//   urls: ['<all_urls>']
// })

// 请求完成拦截
chrome.webRequest.onCompleted.addListener((details) => {
  console.log('谷歌拦截')
  console.log(details);
}, { urls: [
  'http://10.26.2.248:*/*'
]})

// 电影港网站添加去广告菜单项
chrome.runtime.onInstalled.addListener(({ reason }) => {
  if (reason === 'install' || reason === 'update'){
    chrome.contextMenus.create({
      id: contextMenusId.DYGANG,
      title: '去掉广告',
      contexts: [ 'all' ],
      documentUrlPatterns: dygangUrls
    })
  }
});

// 监听点击菜单项发送去广告消息
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
  console.log('监听到菜单点击')
  console.log(info)
  if (info.menuItemId === contextMenusId.DYGANG){
    const datas = await chrome.tabs.sendMessage(tab.id, {
      id: contextMenusId.DYGANG
    });
    console.log(`点击右键去广告后台接受到的返回消息: ${datas}`);
  }
});
